from os import path
import os
import struct

def merge_all_bin(dir = ".", out_file = "All.bin"):
    file_list = os.listdir(dir)
    txt = open("address.log", "w")
    start_addr = 0
    exclude_file = [out_file, "L42AP_FONT_F04_Build09.bin", "L42AP_FONT_F04_Build09.bin"]
    f = open(out_file, "wb")
    for name in file_list:
        if name in exclude_file:
            continue
        ext = path.splitext(path.split(name)[-1])[-1]
        if ext == ".bin":
            with open(name, "rb") as in_file:
                data = in_file.read()
                txt.write("#define "+path.splitext(path.split(name)[-1])[-2]+" 0x%08x\n"%start_addr)
                f.write(data)
                start_addr = start_addr + len(data)
    txt.close()
    f.close()
    print("total size 0x%08x bytes %f KB %f M"%(start_addr, start_addr/1024, start_addr/(1024*1024)))
    
if __name__ == "__main__":
    merge_all_bin()